package com.kotlin.novel.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.kotlin.novel.R
import com.kotlin.novel.imageloader.ImageLoader
import com.kotlin.novel.model.BookModel
import com.kotlin.novel.ui.BookDetailActivity


class BookListAdapter(var context: Context, var bookModels: ArrayList<BookModel>) : RecyclerView.Adapter<BookListAdapter.ViewHolder>() {

    private var maxEnum=30
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivCover: ImageView = itemView.findViewById(R.id.iv_item_cover)
        var tvName: TextView = itemView.findViewById(R.id.tv_item_name)
        var tvAuthor: TextView = itemView.findViewById(R.id.tv_item_author)
        var tvDesc: TextView = itemView.findViewById(R.id.tv_item_desc)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_book, parent, false))
    }

    override fun getItemCount(): Int {
        return bookModels.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val bookModel = bookModels[position]
        holder.tvName.text = bookModel.name
        holder.tvAuthor.text = "作者：" + bookModel.bookAuthor

        bookModel.desc=bookModel.desc.replace("\n","")
        if (bookModel.desc.length>maxEnum){
            holder.tvDesc.text=bookModel.desc.substring(0,maxEnum)+"..."
        }
        else{
            holder.tvDesc.text=bookModel.desc
        }


        ImageLoader.loadImage(context as Activity, holder.ivCover, bookModel.coverUrl)
        holder.itemView.setOnClickListener(View.OnClickListener {
            context.startActivity(Intent(context, BookDetailActivity::class.java)
                    .putExtra("name",bookModel.name)
                    .putExtra("bookUrl",bookModel.url))
        })
    }

}